﻿//Thomas Arvidsson 761027-5617 @ Örebro/Sweden 2011/2012

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Text.RegularExpressions;

namespace PasswordGenerator
{
    public partial class MailPage : PhoneApplicationPage
    {
        

        public MailPage()
        {
            InitializeComponent();

            var a = App.Current as App;
            string password = a.PassWord;
            txtBlockPassword.Text = password;
        }

        private void btnMailMe_Tap(object sender, GestureEventArgs e)
        {
            SendPasswordWithMail();
        }

        /// <summary>
        /// Get the password from property, create a mailtask, fill with values and do neccesary checks before mailing
        /// </summary>
        private void SendPasswordWithMail()
        {
            string mail = txtBoxEmail.Text;
            if (TestIfValidEmail(mail))
            {
                var a = App.Current as App;
                string password = a.PassWord;

                //txtBlockPassword.Text = password;

                EmailComposeTask emailTask = new EmailComposeTask();
                emailTask.Subject = "Password from Password App";
                emailTask.Body = "This is the new password! \nPlease be aware of that its not a good idea to store important passwords in your mailbox." +
                                    "\nOnly consider this as a temporary storage until a safe storage is found, and then it's a good idea to delete this mail!\nIts your own responsibility to safely store sensitive information.\n\nPassword: "
                                    + password;
                emailTask.To = mail;

                if (MessageBox.Show("Please confirm your mail", mail, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    emailTask.Show();
                else { }//Do nothing
            }
            else
                MessageBox.Show("Not a valid email, please check your entry");                
        }

        /// <summary>
        /// Tests parameter with regex if it is a valid email-pattern
        /// </summary>
        /// <param name="mail">String of mail to check</param>
        /// <returns>bool</returns>
        private bool TestIfValidEmail(string mail)
        {
            string regexPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                                + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                                + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
				                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
            Regex re = new Regex(regexPattern);

            if (mail != null)
                return Regex.IsMatch(mail, regexPattern);
            else
                return false;
        }
    }
}